package com.arron.algorithm.leetcodetop100.字符串;

public class 最长公共前缀 {




    public String longestCommonPrefix(String[] strs) {
        String prefix = strs[0];


        for (int i = 1; i < strs.length; i++) {

            prefix = commonPrefix(prefix,strs[i]);

            if (prefix == null){
                return "";
            }

        }
        return prefix;
    }

    private String commonPrefix(String prefix, String str) {

        int minLen = Math.min(prefix.length(),str.length());

        int count = 0;
        for (int i = 0; i < minLen; i++) {

            if (prefix.charAt(i) != str.charAt(i)){
                break;
            }
            count++;
        }
        return count == 0? null : prefix.substring(0,count);
    }

    public static void main(String[] args) {
        最长公共前缀 longcommonPrefix = new 最长公共前缀();

        String[] strs = {"dog","racecar","car"};

        System.out.println(longcommonPrefix.longestCommonPrefix(strs));
    }

}
